package com.sfh.dog.util;

import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.util.SparseArray;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.sfh.dog.DogApplication;
import com.sfh.dog.R;

public class ViewUtil
{
	public static  String getArray(SparseArray<String> array)
	{
		
		final int size = array.size();
		if (size == 0)
		{
			return "";
		}
		if (size == 1)
		{
			return array.valueAt(0);
		}
		StringBuffer content = new StringBuffer();
		for (int i=0; i<size; i++)
		{
			content.append(array.valueAt(i)).append(",");
		}
		
		return content.toString().substring(0, content.length()-1);
	}
	
	public static void setLove(Context context,LinearLayout ly, String content)
	{
		String array[] = content.split(",");
		final int length = array.length;

		int count = length / 5 + ((length % 5) > 0 ? 1 : 0);

		for (int i = 0; i < count; i++)
		{
			LinearLayout mLayout = new LinearLayout(context);
			LinearLayout.LayoutParams params = new LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			params.leftMargin = 8;
			for (int j = i * 5; j < 5 * (i + 1) && j < length; j++)
			{
				TextView tv = getTextView(context,array[j]);
				mLayout.addView(tv, params);
			}
			ly.addView(mLayout);
		}
	}
	
	public static void setLove(Context context, LinearLayout ly,
			SparseArray<String> content)
	{

		int length = content.size();

		int count = length / 5 + ((length % 5) > 0 ? 1 : 0);

		for (int i = 0; i < count; i++)
		{
			LinearLayout mLayout = new LinearLayout(context);
			LinearLayout.LayoutParams params = new LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			params.leftMargin = 8;
			for (int j = i * 5; j < 5 * (i + 1) && j < length; j++)
			{
				TextView tv = getTextView(context, content.valueAt(j));
				mLayout.addView(tv, params);
			}
			ly.addView(mLayout);
		}
	}

	public static TextView getTextView(Context context, String content)
	{
		TextView tv = new TextView(context);
		tv.setWidth(DogApplication.widthItem);
		tv.setBackgroundResource(R.drawable.add_pet_gridview_text_bg);
		tv.setGravity(Gravity.CENTER);
		tv.setPadding(3, 3, 3, 3);
		tv.setEllipsize(TruncateAt.END);
		tv.setSingleLine(true);
		tv.setText(content);
		tv.setTextColor(context.getResources().getColor(R.color.chocolate));
		return tv;
	}
}
